/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2010 huliqing, huliqing.cn@gmail.com
 *
 * This file is part of QFaces.
 * QFaces is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QFaces is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with QFaces.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 这个文件是QFaces的一部分。
 * 您可以单独使用或分发这个文件，但请不要移除这个头部声明信息.
 * QFaces是一个自由软件，您可以自由分发、修改其中的源代码或者重新发布它，
 * 新的任何修改后的重新发布版必须同样在遵守LGPL3或更后续的版本协议下发布.
 * 关于LGPL协议的细则请参考COPYING、COPYING.LESSER文件，
 * 您可以在QFaces的相关目录中获得LGPL协议的副本，
 * 如果没有找到，请连接到 http://www.gnu.org/licenses/ 查看并获取副本。
 *
 * - Author: Huliqing
 * - Contact: huliqing.cn@gmail.com
 * - License: GNU Lesser General Public License (LGPL)
 * - Blog and source code availability: http://www.huliqing.name/
 */

Q.F.UIAjaxSupport = {
    init : function(clientId, componentClass, listenerExp, ref, _for, event, onStart, onProcess, onFinish, eventButton, delay) {
        Q.F.find(clientId);
        var ajaxSupport = new Q.F.AjaxSupport(clientId, componentClass, listenerExp, ref, _for, event, onStart, onProcess, onFinish, eventButton, delay);
        Q.F.put(clientId, ajaxSupport);
    }
}
Q.F.AjaxSupport = function(clientId, componentClass, listenerExp, ref, _for, event, onStart, onProcess, onFinish, eventButton, delay) {
	this.clientId = clientId;	// 组件客户端ID
	this.componentClass = componentClass;
	this.listenerExp = listenerExp;
	this.eventButtonObj = Q.get(eventButton);
	this.event = event;
	this.onStart = new Function(onStart);
	this.onProcess = new Function(onProcess);
	this.onFinish = new Function(onFinish);
	this.inAjax = false;	// 标识当前是否正在进行ajax请求.当用户多次快速点击进行ajax请求时可能出现Ajax请求及Process混乱的问题。使用该属性以保证Ajax请求有序进行
	this.refArr = makeIds(ref);
	this.forArr = makeIds(_for);
	this.result = null;	// 用于存放每次ajax调用之后的结果,所以result并不固定值
	this.delay = parseInt(delay, 10);	// 单位毫秒
	
	function makeIds(ids) {
		var array = new Array();
		if (ids == null || ids == "")
			return array;
		if (ids.indexOf(",") != -1) {
			return ids.split(",");
		} else {
			array[0] = ids;
			return array;
		}
	}
	// ---- Inteface
	
	this.getResult = function() {
		return this.result;
	}
	
	// ---- Init
	
	this.init = function() {
		var _self = this;
		if (_self.event == "onload" || _self.event == "onLoad") {
			Q.U.setTimeout(_self.ajaxInvoke, _self.delay, _self);
		} else {
			if (_self.eventButtonObj != null) {
				Q.U.attachEvent(_self.eventButtonObj, _self.event, function(){_self.ajaxInvoke()});
			}
		}
	}
	this.ajaxInvoke = function(ajaxSupport) {
		// 如果onStart函数存在，并返回false，则不进行Ajax请求
		var _self = null;
		if (ajaxSupport != null) {
			_self = ajaxSupport;
		} else {
			_self = this;
		}
		if (_self.onStart != null) {
			var isReady = _self.onStart.apply();
			if (isReady != null && isReady == false) {
				return;
			}
		}
		
		// 如果前一个Ajax请求未结束，则不进行请求
		if (_self.inAjax) {
			alert("Ajax invoke in processing! please wait...");
			return;
		}
		
		_self.inAjax = true;
		var ajax = Q.F.Ajax.build(_self.clientId);
		ajax.put("componentClass", _self.componentClass);
		ajax.put("listenerExp", _self.listenerExp);
		if (_self.refArr != null && _self.refArr.length > 0) {
			for (var i = 0; i < _self.refArr.length; i++) {
				if (_self.refArr[i] == "") {continue;} // Fix Bug
				var refObj = Q.get(_self.refArr[i]);
				if (refObj != null) {
					ajax.put("refValue" + i, refObj.value);
				} else {
					alert("Ref Object not found by ref id:" + _self.refArr[i]);					
				}
			}
		}
		ajax.put("refCount", _self.refArr.length);
		
		var _process = function() {
			_self.process(_self);
		}
	    ajax.setProcess(_process);
	    ajax.get();
	    if (_self.onProcess != null) {
	    	_self.onProcess.apply();
	    }
	}
	
	this.process = function(ajaxSupport) {
		ajaxSupport.inAjax = false;
		ajaxSupport.result = Q.F.Ajax.find(ajaxSupport.clientId).request.responseText;
		var forArr = ajaxSupport.forArr;
		if (forArr != null && forArr.length > 0) {
			for (var i = 0; i < forArr.length; i++) {
				if (forArr[i] == "") {continue;} // Fix Bug
				var target = Q.get(forArr[i]);
				if (target == null) {
					alert("Target object not found by id=" + forArr[i]);
					continue;
				}
				setValue(target, ajaxSupport.result);
			}
		}		
		ajaxSupport.onFinish.apply();
		
		function setValue(targetObj, value) {
			var tagName = targetObj.tagName;
			if (tagName != null) {
				if (tagName == "INPUT" || tagName == "input") {
					targetObj.value = value;
				} else if (tagName == "DIV" || tagName == "div" || tagName == "SPAN" || tagName == "span") {
					targetObj.innerHTML = value;
				} else {
					targetObj.value = value;
				}
			}
		}
	}
	this.init();
}